//
//  XYTOrderEnteringViewController.m
//  XYTPayOrderSystem
//
//  Created by wupeng on 2017/8/23.
//  Copyright © 2017年 wupeng. All rights reserved.
//

#import "XYTOrderEnteringViewController.h"

@interface XYTOrderEnteringViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *orderNumField;

@property (nonatomic, strong) UITextField *telNumField;


@end

@implementation XYTOrderEnteringViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单录入";
    
    [self setupUI];
    
}

- (void)setupUI{
    UILabel *orderNumLab = [[UILabel alloc] initWithFrame:CGRectMake(30, 74, 100, 25)];
    orderNumLab.textColor = [UIColor colorWithRed:69.0f/255.0 green:140.0f/255.0 blue:200.0f/255.0 alpha:1];
    orderNumLab.text = @"订单号:";
    orderNumLab.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:orderNumLab];
    
    UITextField *orderNumField = [[UITextField alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(orderNumLab.frame) + 5, kScreen_Width - 30*2, 40)];
    orderNumField.borderStyle = UITextBorderStyleRoundedRect;
    orderNumField.font = [UIFont systemFontOfSize:16.0f];
    orderNumField.textAlignment = NSTextAlignmentLeft;
    orderNumField.keyboardType = UIKeyboardTypeNumberPad;
    orderNumField.clearButtonMode = UITextFieldViewModeWhileEditing;
    orderNumField.delegate = self;
    orderNumField.placeholder = @"请输入订单号后七位数字";
    [self.view addSubview:orderNumField];
    self.orderNumField = orderNumField;
    
    UILabel *telNumLab = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(orderNumField.frame) + 10, 100, 25)];
    telNumLab.textColor = [UIColor colorWithRed:69.0f/255.0 green:140.0f/255.0 blue:200.0f/255.0 alpha:1];
    telNumLab.text = @"手机号:";
    telNumLab.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:telNumLab];
    
    UITextField *telNumField = [[UITextField alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(telNumLab.frame) + 5, kScreen_Width - 30*2, 40)];
    telNumField.borderStyle = UITextBorderStyleRoundedRect;
    telNumField.font = [UIFont systemFontOfSize:16.0f];
    telNumField.placeholder = @"请输入手机号";
    telNumField.textAlignment = NSTextAlignmentLeft;
    telNumField.keyboardType = UIKeyboardTypeNumberPad;
    telNumField.clearButtonMode = UITextFieldViewModeWhileEditing;
    telNumField.delegate = self;
    [self.view addSubview:telNumField];
    self.telNumField = telNumField;
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    submitBtn.frame = CGRectMake(30, CGRectGetMaxY(telNumField.frame) + 15, kScreen_Width - 30*2, 40);
    submitBtn.backgroundColor = [UIColor colorWithRed:69.0f/255.0 green:140.0f/255.0 blue:200.0f/255.0 alpha:1];
    submitBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitBtn setTitle:@"录入" forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submitOrder) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitBtn];
    
}
#pragma mark - private Methods
//提交订单
- (void)submitOrder{
    [UIView showHUD:nil];
    NSString *phone = self.telNumField.text;
    NSString *orderNo = self.orderNumField.text;
    NSString *signStr = [NSString stringWithFormat: @"%@%@%@%@",AppKey,AppSecret,phone,orderNo];
    NSString *md5Sign = [Md5Util md5:signStr];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params xyt_setObj:phone forKey:@"phone"];
    [params xyt_setObj:orderNo forKey:@"orderNo"];
    [params xyt_setObj:md5Sign forKey:@"sign"];
    XYTWeakSelf(self)
    [XYTNetRequest POST:OrderEnterUrl parameters:params isNeedKey:NO success:^(id responseObject) {
        [UIView hideHUD];
        [UIView showCustomToast:@"订单录入成功"];
    } failure:^(NSString *errorMsg) {
        XYTStrongSelf(weakSelf)
        [UIView hideHUD];
        [strongSelf showAlertWithTitle:errorMsg];
        
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.orderNumField resignFirstResponder];
    [self.telNumField resignFirstResponder];
}

#pragma mark - textFieldDelgate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (string.length == 0) return YES;
    
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    if (textField == self.orderNumField) {
        if (existedLength - selectedLength + replaceLength > 7) {
            return NO;
        }
    }
    else if (textField == self.telNumField){
        if (existedLength - selectedLength + replaceLength > 11) {
            return NO;
        }
    }
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
